<script setup>
// 文章添加与编辑的抽屉弹层
import { ref } from 'vue'
import SelectBase from '@/components/SelectBase.vue'
import { artSubmitService, artGetFullService, artResetService } from '@/api/article'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { baseURL } from '@/utils/request'
const emit = defineEmits(['success'])
const isVisible = ref(false)
const defaultMessage = ref({
  title: '',
  cate_id: '',
  content: 'test',
  cover_img: '',
  state: ''
})
// 组件入口
const open = async (obj) => {
  isVisible.value = true
  defaultMessage.value = {
      title: '',
      cate_id: '',
      content: '',
      cover_img: '',
      state: ''
    }
  // 如果是编辑状态
  if (obj.id) {
    const res = await artGetFullService(obj.id)
    res.data.cover_img = baseURL + res.data.cover_img
    defaultMessage.value = {
      ...res.data
    }
    // console.log(defaultMessage.value)
  }
  // 如果是添加状态
  else {
    
  }
  // console.log(obj)
}

// 用户头像预上传
const previewImg = (file) => {
  defaultMessage.value.cover_img = URL.createObjectURL(file.raw)
  // console.log(URL.createObjectURL(file.raw))
}
// 发送表单数据
const artSubmit = async (str) => {
  defaultMessage.value.state = str
  const formData = new FormData()
  for (let k in defaultMessage.value) {
    formData.append(k, defaultMessage.value[k])
  }
  // 如果是发布全新文章
  if (defaultMessage.value.id) {
    await artSubmitService(formData)
    ElMessage.success('发布文章成功')
    isVisible.value = false
    emit('success', 'submit')
  }
  // 如果是修改现有文章
  else {
    const res = await artResetService(formData)
    console.log(res)
  }
}

defineExpose({
  open
})
</script>

<template>
  <el-drawer v-model="isVisible" size="40%">
    <template #header>
      <h3>添加文章</h3>
    </template>
    <el-form :model="defaultMessage">
      <el-form-item label="文章标题">
        <el-input placeholder="请输入标题" v-model="defaultMessage.title"></el-input>
      </el-form-item>
      <el-form-item>
        <SelectBase v-model="defaultMessage.cate_id" width="100%"></SelectBase>
      </el-form-item>
      <el-form-item label="文章封面">
        <el-upload
          class="avatar-uploader"
          :auto-upload="false"
          :on-change="previewImg"
        >
          <img v-if="defaultMessage.cover_img" :src="defaultMessage.cover_img" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容">
        <div style="width:100%">
          <quill-editor
            theme="snow"
            v-model:content="defaultMessage.content"
            content-type="html"
            style="min-height:200px"
          ></quill-editor>
        </div>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="artSubmit('已发布')">发送</el-button>
        <el-button type="info" @click="artSubmit('草稿')">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
// 用户头像预上传
.avatar-uploader {
  .avatar {
    width:150px;
    height:150px;
    border:3px dotted gray;
    border-radius:5px;
  }
  .avatar-uploader-icon {
    width:150px;
    height:150px;
    border:3px dotted gray;
    border-radius:5px;
  }
}
</style>